package com.neusoft.lohas.modules.ivm.entity.business;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.neusoft.lohas.common.persistence.IdEntity;
import com.neusoft.lohas.common.utils.DateUtils;
import com.neusoft.lohas.modules.ivm.entity.merchant.Merchant;
import com.neusoft.lohas.modules.ivm.entity.product.ProductClassify;

/**
 * 商圈活动类
 * @author jackie.liu
 * @version 2015-01-29
 */
@Entity
@Table(name = "ivm_business_activity")
@DynamicInsert
@DynamicUpdate
public class Activity extends IdEntity<Activity> {

	private static final long serialVersionUID = 1L;
	private String type; //活动类型
	private String name; //活动名称
	private String assisTitle; // 副标题
	private Merchant merchant; // 商户信息
	private Date activityDate; //活动日期
	private String startTime; //开始时间
	private String endTime; //结束时间
	private String logoUrl; //活动图片
	private String status;  //活动状态
	private List<ActivityTime> timeList = Lists.newArrayList(); //日期列表
	
	public Activity() {
		super();
	}

	public Activity(String id) {
		this();
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAssisTitle() {
		return assisTitle;
	}

	public void setAssisTitle(String assisTitle) {
		this.assisTitle = assisTitle;
	}

	@ManyToOne
	@JoinColumn(name="merchant_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@JsonIgnore
	@NotNull(message="所属商户信息不能为空")
	public Merchant getMerchant() {
		return merchant;
	}

	public void setMerchant(Merchant merchant) {
		this.merchant = merchant;
	}

	@Transient
	public Date getActivityDate() {
		return activityDate;
	}

	public void setActivityDate(Date activityDate) {
		this.activityDate = activityDate;
	}

	@Transient
	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	@Transient
	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getLogoUrl() {
		return logoUrl;
	}

	public void setLogoUrl(String logoUrl) {
		this.logoUrl = logoUrl;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "activity")
	@Where(clause="del_flag='"+DEL_FLAG_NORMAL+"'")
	@OrderBy(value="activity_date , startTime, endTime ASC")
	@NotFound(action = NotFoundAction.IGNORE)
	@JsonIgnore
	public List<ActivityTime> getTimeList() {
		return timeList;
	}

	public void setTimeList(List<ActivityTime> timeList) {
		this.timeList = timeList;
	}
	
	@Transient
	public String getTimeStr(){
		if (timeList.size()>0) {
//			StringBuffer sb = new StringBuffer();
//			for (int i=0; i<timeList.size(); i++) {
//				ActivityTime time = timeList.get(i);
//				if (i==(timeList.size()-1)) {
//					sb.append(DateUtils.formatDateTime(time.getActivityDate(), "yyyy-MM-dd")+" "+time.getStartTime()+"~"+time.getEndTime());
//				}else {
//					sb.append(DateUtils.formatDateTime(time.getActivityDate(), "yyyy-MM-dd")+" "+time.getStartTime()+"~"+time.getEndTime()+"</br>");
//				}
//			}
//			return sb.toString();
			Map<String, String> map = new LinkedHashMap<String, String>();
			for (ActivityTime time : timeList) {
				if (map.containsKey(DateUtils.formatDateTime(time.getActivityDate(), "yyyy-MM-dd"))) {
					map.put(DateUtils.formatDateTime(time.getActivityDate(), "yyyy-MM-dd"), map.get(DateUtils.formatDateTime(time.getActivityDate(), "yyyy-MM-dd"))+","+time.getStartTime()+"~"+time.getEndTime());
				}else {
					map.put(DateUtils.formatDateTime(time.getActivityDate(), "yyyy-MM-dd"), DateUtils.formatDateTime(time.getActivityDate(), "yyyy-MM-dd")+" "+time.getStartTime()+"~"+time.getEndTime());
				}
			}
			StringBuffer rtn = new StringBuffer();
			int i = 0;
			for (String string : map.values()) {
				if (i==map.size()-1) {
					rtn.append(string);
				}else {
					rtn.append(string+"<br/>");
				}
			}
			return rtn.toString();
		}else {
			return "暂无";
		}
	}
	
}
